package com.elitel.frame.base.service.impl;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.BaseReptempletService;
import com.elitel.frame.core.util.FileControlUtil;
import com.elitel.frame.main.dao.BaseReptemplateMapper;
import com.elitel.frame.main.dao.CfgExtreportMapper;
import com.elitel.frame.main.dao.SysPrjserviceMapper;
import com.elitel.frame.main.dao.ext.ExtBaseRepaddonsMapper;
import com.elitel.frame.main.dao.ext.ExtBaseReptempletMapper;
import com.elitel.frame.main.entity.BaseReptemplate;
import com.elitel.frame.main.entity.CfgExtreport;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.PrjServiceExt;

@Service
public class BaseReptempletServiceImpl implements BaseReptempletService {
	
	@Autowired
	private BaseReptemplateMapper baseReptempatetMapper;
	@Autowired
	private ExtBaseReptempletMapper extBaseReptemplateMapper;
	@Autowired
	private CfgExtreportMapper cfgExtreportMapper;
	@Autowired
	private SysPrjserviceMapper sysPrjserviceMapper;
	
	@Override
	public BaseReptemplate selectBaseRepTemplateByPrimary(String tempid) {
		BaseReptemplate selectByPrimaryKey = baseReptempatetMapper.selectByPrimaryKey(tempid);
		return selectByPrimaryKey;
	}

	@Override
	public List<BaseReptemplate> selectReptempleList() {
		List<BaseReptemplate> list = extBaseReptemplateMapper.selectReptempleList();
		return list;
	}

	@Override
	public Long selectCount(String seachTxt) {
		Long count = extBaseReptemplateMapper.selectCount(seachTxt);
		return count;
	}

	@Override
	public List<BaseReptemplate> queryReptemplateByPaging(String seachTxt, String pageNo, String pageSize) {
		Integer start = (Integer.valueOf(pageNo) - 1) * Integer.valueOf(pageSize);
		Integer end = Integer.valueOf(pageNo) * Integer.valueOf(pageSize);
		List<BaseReptemplate> list = extBaseReptemplateMapper.queryReptemplateByPaging(seachTxt, start, end);
		return list;
	}

	@Override
	public int insertReptemplate(BaseReptemplate BaseReptemplate) {
		int size = baseReptempatetMapper.insertSelective(BaseReptemplate);
		return size;
	}

	@Override
	public BaseReptemplate selectReptemplateByKey(String guid) {
		BaseReptemplate selectByPrimaryKey = baseReptempatetMapper.selectByPrimaryKey(guid);
		return selectByPrimaryKey;
	}

	@Override
	public int updateReptemplate(BaseReptemplate baseReptemplate) {
		int size = baseReptempatetMapper.updateByPrimaryKeySelective(baseReptemplate);
		return size;
	}

	@Override
	public void deleteReptemplate(String guid, HttpServletRequest request) throws Exception{
		BaseReptemplate template = baseReptempatetMapper.selectByPrimaryKey(guid);
		String jasperurl = request.getSession().getServletContext().getRealPath(template.getTemplateUrl());
		String jrxmlurl = request.getSession().getServletContext().getRealPath(template.getJrxmlUrl());
		if(jasperurl != null && jasperurl != ""){
			FileControlUtil.deleteFile(jasperurl);
		}
		if(jrxmlurl != null && jrxmlurl != ""){
			FileControlUtil.deleteFile(jrxmlurl);
		}
		baseReptempatetMapper.deleteByPrimaryKey(guid);
//		int size = extBaseRepaddonsMapper.deleteByTempId(guid);
		
	}

	@Override
	public BaseReptemplate getReptemplateByServiceid(String serviceid) {
		BaseReptemplate BaseReptemplate = extBaseReptemplateMapper.getReptemplateByServiceid(serviceid);
		return BaseReptemplate;
	}

}
